package com.codemeta.bokit.mybatis.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.codemeta.bokit.mybatis.core.handler.DefaultMetaObjectHandler;
import com.codemeta.bokit.props.MybatisProps;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis-plus自动装配
 *
 * @author hice
 * @date 2021/11/19
 */
@Slf4j
@Configuration
@MapperScan(
    annotationClass = Mapper.class,
    value = {"${bokit.mybatis-plus.base-packages:'com.codemeta.bokit'}"},
    lazyInitialization = "${bokit.mybatis-plus.lazy-initialization:false}"
)
@EnableConfigurationProperties(MybatisProps.class)
public class MybatisAutoConfig {

    @Bean
    @ConditionalOnMissingBean(MybatisPlusInterceptor.class)
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        log.debug("【bokit】开启mybatis-plus分页拦截器");
        return interceptor;
    }

    @Bean
    @ConditionalOnMissingBean(DefaultMetaObjectHandler.class)
    public DefaultMetaObjectHandler mybatisMetaObjectHandler() {
        // 自动填充参数类
        log.debug("【bokit】开启mybatis-plus数据自动填充处理器");
        return new DefaultMetaObjectHandler();
    }
}